package com.pdf.demo.controller;

import com.google.gson.internal.LinkedTreeMap;
import com.pdf.demo.bean.Person;
import com.pdf.demo.util.JsonUtil;
import com.pdf.demo.util.PdfUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class DemoController {

    @RequestMapping("/hello")
    public String index(ModelMap modelMap) {
        List<Person> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Person("张三",i,"男"));
        }
        modelMap.addAttribute("persons",list);
        modelMap.addAttribute("name", "张三");
        return "pdf";
    }

    @GetMapping("/test")
    public String test(ModelMap mmap) throws Exception {
        File file = new File("D:/json.txt");
        InputStream inputStream = new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer json = new StringBuffer();
        String temp;
        while (!StringUtils.isEmpty((temp = bufferedReader.readLine()))) {
            json.append(temp);
        }
        String jsonStr = json.toString();
        LinkedTreeMap<String, Object> map = JsonUtil.fromJson(jsonStr);
        /** json数据转PDF文件 **/
        String filePath = "D:/PDF";
        String fileName = "/" + "222.pdf";
        PdfUtil.generatePdf(map, filePath, fileName);
//        String url = serverConfig.getUrl() + FileUploadUtils.getPathFileName(filePath, fileName);
//        mmap.put("url", url);
        return "index";
    }
}
